/**
 * 拦截器
 */
const jwtUtil = require('./jwtUtils');
const config = require('./config');

const interceptor = async (req, res, next) => {
  // 过滤
  const path = req.path;
  if (path.startsWith('/users/login')) {
    next();
    return;
  }
  const token = req.headers['authorization'];
  const result = await jwtUtil.verifysync(token, config.secretkey);
  if (result.state === 0) {
    // 验证失败
    res.send(result);
  } else {
    // 验证成功
    next();
  }
}

module.exports = {
  interceptor,
}